/* eslint-disable no-void */
import { defaultNumField, defaultArrField } from '../../fieldType';
import { doubleNumber, dateTime, date, chinese, number1, english, phoneOther, mobile, landline } from '@/afterSales/utils/validate'
import { isUndef } from '@/afterSales/utils/common'
export const inputField = ['text', 'phone', 'email']
export enum textFormat { all = 1, chinese, english, number }
// 必填校验
export function commonRule (item) {
  const type = defaultArrField.includes(item.fieldType) ? 'array' : defaultNumField.includes(item.fieldType) ? 'number' : void 0;
  const v = item.require
  if (v) {
    return [{ required: true, message: '必填', trigger: 'change', type }]
  }
  return []
}
const validatorFixed = (rule: any, value: string, callback: any) => {
  if (!value) {
    return callback()
  }
  if ((Number(value).toFixed(rule.decimalPoint) !== value)) {
    callback(new Error('小数点后保留' + rule.decimalPoint + '位'))
  } else {
    callback()
  }
}
// 数字校验
export function numberRule (item: any) {
  const rules: any = []
  const { numberRange, maxNumber, minNumber, decimalPoint } = item
  rules.push({ message: '必须为数字', pattern: doubleNumber, trigger: ['blur'] })
  if (numberRange === 2) {
    rules.push({ trigger: 'blur', validator: validatorRange, maxNumber, minNumber })
  }
  rules.push({ trigger: 'blur', validator: validatorFixed, decimalPoint })
  return rules
}
const validatorRange = (rule: any, value: string, callback: any) => {
  if (!value) {
    callback()
  }
  if (!isUndef(rule.maxNumber) && (value > rule.maxNumber)) {
    callback(new Error('最大值不超过' + rule.maxNumber))
  } else if (!isUndef(rule.minNumber) && (value < rule.minNumber)) {
    callback(new Error('最小值不小于' + rule.minNumber))
  } else {
    callback()
  }
}
/* 判断最后一个级是不是叶子节点 */
function isLeaf (ids, options) {
  ids = [...ids]
  for (let i = 0; i < options.length; i++) {
    const item = options[i]
    if (item.key === ids[0]) {
      ids.shift()
      if (!ids.length) {
        // 排除已删除节点
        return !(item.children || []).find((one) => one.isDelete !== 1)
      }
      return isLeaf(ids, item.children || [])
    }
  }
}
export const validatorCascade = (rule: any, value: string, callback: any) => {
  const field = rule.item
  if (value.length >= field.requireLevel) {
    callback()
  } else {
    if (isLeaf(value, field.options)) {
      callback()
    } else {
      callback(new Error(field.requireLevel + '级必填'))
    }
  }
}
// 级联校验
export function cascadeRule (item, rules) {
  if (rules.length) {
    rules.push({ trigger: 'blur', validator: validatorCascade, item })
    return rules
  }
  return []
}
// 日期校验
export function dateRule (item) {
  const rules: any = []
  const pattern = item.dateType === 1 ? date : dateTime;
  const message = item.dateType === 1 ? '必须为时间格式' : '必须为时间+日期格式';
  rules.push({ message, pattern, trigger: ['blur'] })
  return rules
}
export function itemToRule (item) {
  const rules = commonRule(item)
  if (inputField.includes(item.fieldType)) {
    return rules.concat(inputRule(item))
  } else if (item.fieldType === 'number') {
    return rules.concat(numberRule(item))
  } else if (item.fieldType === 'dateTime') {
    return rules.concat(dateRule(item))
  } else if (item.fieldType === 'cascadeSelect') {
    return cascadeRule(item, rules)
  } else {
    return rules
  }
}

// 文本校验
export function inputRule (item) {
  const rules: any = []
  if (item.fieldType === 'text') {
    if (item.format === 1) {
      if (item.contentLengthRange === 2 && item.maxContentLength) {
        rules.push({ type: 'string', max: item.maxContentLength, message: '最大长度不超过' + item.maxContentLength + '字符', trigger: ['blur'] })
      }
      if (item.contentLengthRange === 2 && item.minContentLength) {
        rules.push({ type: 'string', min: item.minContentLength, message: '最小长度不少于' + item.minContentLength + '字符', trigger: ['blur'] })
      }
    }
    if (item.format === 3) {
      rules.push({ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] })
    } else if (item.format === 2) {
      let pattern: RegExp | null = null
      const message = '请输入正确号码'
      pattern = mobile
      rules.push({ type: 'string', pattern, message, trigger: ['blur'] })
    }
  } else if (item.fieldType === 'email') {
    rules.push({ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] })
  } else if (item.fieldType === 'phone') {
    let pattern: RegExp | null = null
    let message = '请输入正确号码'
    switch (item.phoneType) {
      case 1:
        pattern = mobile
        break;
      case 2:
        pattern = landline
        message = '请输入正确号码(区号-电话号码-分机号码)s'
        break;
      case 3:
        pattern = phoneOther
        break;
    }
    rules.push({ type: 'string', pattern, message, trigger: ['blur'] })
  }
  return rules
}
